﻿/*
*   描述：重点监控预选方案操作
*/
var programid; //
var programtype = 'TBSXDZYL';
var isInit = true;

function InitSelect(type, renderTo) {
    ClearSelect(renderTo);
    $.ajax({ url: 'TBSChannel.ashx',
        cache: false,
        dataType: 'json',
        type: 'POST',
        data: { getprogram: '', programtype: programtype },
        success: function(dt) {
            if (dt && dt.rows) {
                CreateSelect(dt, renderTo);
            }
            else {
                $('<option>').attr({ 'text': '---请选择方案---', 'value': '' }).html('---请选择方案--')
        .appendTo($('#' + renderTo));
            }
            isInit = false;
        },
        error: function(e) {
        }
    });
}
function CreateSelect(dt, renderTo) {
    for (var i = 0, length = dt.rows.length; i < length; i++) {
        $('<option>').attr({ 'text': dt.rows[i]['PROGRAMNAME'], 'value': dt.rows[i]['ID'], 'selected': (dt.rows[i]['ISUSED'] == '1' ? 'selected' : '') })
        .html(dt.rows[i]['PROGRAMNAME'])
        .appendTo($('#' + renderTo));
        if (dt.rows[i]['ISUSED'] == '1') {
            programid = dt.rows[i]['ID'];
        }
    }
    if (dt.rows.length == 1) {
        programid = dt.rows[0]['ID'];
    }
    if (isInit) {
        GetTbsList(programid, 'zhwg_' + programtype);
    }
}
function ClearSelect(renderTo) {
    $('#' + renderTo).html('');
}
function GetTbsList(programid, cn) {
    $.ajax({ url: 'TBSChannel.ashx',
        cache: false,
        dataType: 'json',
        type: 'POST',
        data: { getprogramtbs: '', programid: programid },
        success: function(result) {
            if (result && result.result) {
                setTBSToCookie(cn, result.result);
                setNodeCheckedInit();
                if (programtype == 'TBSMainChannel') {
                    window.parent.document.getElementById('ImpMonitorIndexRight').contentWindow.reInitPage(isInit);
                }
                if (programtype == 'TBSXN') {
                    window.parent.document.getElementById('ImpMonitorIndexRight').contentWindow.initPage();
                }
                if (programtype == 'TBSXDZYL') {
                    setStatic();
                }
            }
        },
        error: function(e) {
        }
    });
}
/*将选中的基站TBS字符串保存到COOKIE中*/
function setTBSToCookie(cookiename, tbslist) {
    window.parent.SetCookie(cookiename, tbslist);
}

function getTreeChecked(nodeTree, pid) {
    var sqlInsert = "";
    var pid = pid;
    if (nodeTree.getNodes().length > 0) {
        for (var l = 0, length = nodeTree.getNodes().length; l < length; l++) {
            if (nodeTree.getNodes()[l].getChildNodes().length > 0) {
                for (var nodeOne = 0, count = nodeTree.getNodes()[l].getChildNodes().length; nodeOne < count; nodeOne++) {
                    if (nodeTree.getNodes()[l].getChildNodes()[nodeOne].getChecked() == true) {
                        var neid = nodeTree.getNodes()[l].getChildNodes()[nodeOne].getValue();
                        sqlInsert += "insert into programconfigtbs values('" + nodeOne + "','" + pid + "','" + neid + "');";
                    }
                }
            }
        }
    }
    return sqlInsert;
}

$(function() {
    programtype = getValueOfURLParamter('programtype');
    InitSelect(programtype, 'selProgram');
    $('#selProgram').bind('change', function() {
        programid = $('#selProgram :selected').val();
        // setNodeUnChecked();
        var sql = 'update  programconfig set isused=\'0\' where PROGRAMTYPE=\'' + programtype
        + '\';update  programconfig set isused=\'1\' where PROGRAMTYPE=\'' + programtype + '\' and ID=\'' + programid + '\';';
        UpdateProgram(sql);
    });
    $('#btnAdd').bind('click', function() {
        $('<div>').attr({ 'id': 'programAdd' }).css({ 'position': 'absolute', 'z-index': '10', 'width': '100%', 'padding-top': '30px', 'height': '660px', 'top': '0px', 'left': '0px', 'background-color': 'White', 'border': '#6990AF 1px solid' })
        .append($('<label>').html('方案名称'))
        .append($('<input>').attr({ 'type': 'text', 'id': 'txtProgram' }).css({ 'width': '120px', 'text-align': 'center' }))
        .append($('<br>'))
        .append($('<input>').attr({ 'id': 'btnOK', 'type': 'button', 'value': '确定' }).bind('click', function() {
            programid = new Date().format('yyyyMMddhhmssSq');
            var sql = 'update  programconfig set isused=\'0\' where PROGRAMTYPE=\'' + programtype + '\'; insert into programconfig values (\'' + programid + '\',\'' + $('#txtProgram').val()
        + '\',\'' + $('#txtProgram').val() + '\',\'1\',now(),\'\',\'\',\'' + programtype + '\');';
            AddProgram(sql);
            // setNodeUnChecked();
        }))
        .append($('<input>').attr({ 'id': 'btnCalcel', 'type': 'button', 'value': '取消' }).bind('click', function() {
            $('#programAdd').remove();
        }))
        .appendTo($('body'));
    });
    $('#btnSave').bind('click', function() {
        SaveTBS(tree);
    });
    $('#btnDelete').bind('click', function() {
        DeleteProgram(' delete from programconfig where ID=\'' + programid + '\' ');
    });
});
function SaveTBS(tree) {
    var sql = getTreeChecked(tree, programid);
    sql = 'delete from  programconfigtbs where programid=\'' + programid + '\'; ' + sql;
    $.ajax({ url: 'TBSChannel.ashx',
        cache: false,
        dataType: 'json',
        type: 'POST',
        data: { updatetbs: '', sql: sql },
        success: function(result) {
            if (result && result.result) {
                alert('保存成功!');
                InitSelect(programtype, 'selProgram');
                GetTbsList(programid, 'zhwg_' + programtype);
            }
            else {
                alert('保存失败!');
            }
        },
        error: function(e) {
        }
    });
}
function AddProgram(sql) {
    $.ajax({ url: 'TBSChannel.ashx',
        cache: false,
        dataType: 'json',
        type: 'POST',
        data: { updatetbs: '', sql: sql },
        success: function(result) {
            if (result && result.result) {
                $('#programAdd').remove();
                InitSelect(programtype, 'selProgram');
            }
        },
        error: function(e) {
        }
    });
}
function DeleteProgram(sql) {
    $.ajax({ url: 'TBSChannel.ashx',
        cache: false,
        dataType: 'json',
        type: 'POST',
        data: { updatetbs: '', sql: sql },
        success: function(result) {
            if (result && result.result) {
                alert('删除成功!');
                InitSelect(programtype, 'selProgram');
            }
            else {
                alert('删除失败!');
            }
        },
        error: function(e) {
        }
    });
}
function UpdateProgram(sql) {
    $.ajax({ url: 'TBSChannel.ashx',
        cache: false,
        dataType: 'json',
        type: 'POST',
        data: { updatetbs: '', sql: sql },
        success: function(result) {
            GetTbsList(programid, 'zhwg_' + programtype);
        },
        error: function(e) {
        }
    });
}
/*设置节点选中*/
function setNodeUnChecked() {
    var nodes = tree.getNodes();
    for (var i = 0, length = nodes.length; i < length; i++) {
        var childNodes = nodes[i].getChildNodes();
        for (var j = 0, count = childNodes.length; j < count; j++) {
            setChildrenCheckBox(childNodes[j], false);
        }
    }
}